package com.spring.test;

import com.spring.aop.MathCal;
import com.spring.bean.Person;
import com.spring.config.*;
import com.spring.ext.ExtConfig;
import com.spring.service.BookService;
import com.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Description:
 *
 * @author lijie
 * @create 2019-01-14 23:59
 * Update by lijie 2019-01-14 23:59
 */
public class Demo01 {

    @Test
    public void test01(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name:names){
            System.out.println(name);
        }
    }
    @Test
    public void test02(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name:names){
            System.out.println(name);
        }
        Object bean = context.getBean("colorBeanFactory");
        System.out.println(bean.getClass());
        Object bean2 = context.getBean("&colorBeanFactory");
        System.out.println(bean2.getClass());
    }
    @Test
    public void test03(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name:names) {
            System.out.println(name);
        }
        Object cat = context.getBean("cat");
        context.close();
    }
    @Test
    public void test04(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfProperty.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name:names) {
            System.out.println(name);
        }
        Person person = (Person) context.getBean("person");
        System.out.println(person);
        context.close();
    }
    @Test
    public void test05(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigAutowire.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name:names) {
            System.out.println(name);
        }
        //Person person = (Person) context.getBean("person");
        //System.out.println(person);
        BookService bookService = (BookService) context.getBean("bookService");
        System.out.println(bookService);
        //BookDao bookDao = context.getBean(BookDao.class);
        //System.out.println(bookDao);
        context.close();
    }
    @Test
    public void test06(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name:names) {
            System.out.println(name);
        }
        MathCal mathCal = context.getBean(MathCal.class);
        mathCal.dev(2,3);
        context.close();
    }
    @Test
    public void test07(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userService = (UserService) context.getBean("userService");
        userService.insert();
    }
    @Test
    public void test08(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ExtConfig.class);
        context.publishEvent(new ApplicationEvent("我发布的事件") {});
        context.close();
    }
}
